/*
 * Copyright &copy; 2011-2020 lnint Inc. All right reserved.
 * 
 * 修改信息：【与SVN提交信息一致】
 * A: 新增类 王磊 2015-03-20 08:42:32
 */
package com.lnint.jess.generator.dao;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.lnint.jess.common.config.Global;
import com.lnint.jess.common.persistence.BaseDao;
import com.lnint.jess.common.persistence.Parameter;
import com.lnint.jess.generator.entity.GenScene;

/**
 * <P>代码生成场景管理DAO接口</P>
 *
 * @version 2015-03-20
 * @author 王磊 2015-03-20 08:42:32
 * @since JDK 1.6
 */
@Repository
public class GenSceneDao extends BaseDao<GenScene> {
	//以下可以自定义业务 其他复杂处理 DAO
	
	/**
	 * <P>根据表名获得表的 物理字段属性信息</P>
	 * @param tablename 表名;
	 * @return GenScene
	 */
	public List<Object> findTableColumnList(String tablename){
		List<Object> columnList = Lists.newArrayList();	
		//获得数据库类型
		String dbType = Global.getConfig("jdbc.type");
		StringBuffer querySql= new StringBuffer();
		if ("mysql".equals(dbType)){
			//mysql数据库 方言
			querySql.append(" SELECT t.COLUMN_NAME AS name, (CASE WHEN t.IS_NULLABLE = 'YES' THEN '1' ELSE '0' END) AS isNull,"
					+ "  	(t.ORDINAL_POSITION * 10) AS sort,t.COLUMN_COMMENT AS comments,t.COLUMN_TYPE AS jdbcType  "
					+ " FROM information_schema.`COLUMNS` t  "
					+ " WHERE t.TABLE_SCHEMA = (select database()) "
					+ " AND t.TABLE_NAME = upper(:tabName) "
					+ " ORDER BY t.ORDINAL_POSITION ");
		}else if("oracle".equals(dbType)){
			//oracle数据库  方言
			querySql.append(" SELECT t.COLUMN_NAME AS name,"
					+ "  (CASE WHEN t.NULLABLE = 'Y' THEN '1' ELSE '0' END) AS isNull,  "
					+ " (t.COLUMN_ID * 10) AS sort,c.COMMENTS AS comments,  "
					+ " decode(t.DATA_TYPE,'DATE',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')', "
					+ " 'VARCHAR2', t.DATA_TYPE || '(' || t.DATA_LENGTH || ')', "
					+ " 'VARCHAR', t.DATA_TYPE || '(' || t.DATA_LENGTH || ')', "
					+ " 'CHAR', t.DATA_TYPE || '(' || t.DATA_LENGTH || ')', "
					+ " 'NUMBER',t.DATA_TYPE || (nvl2(t.DATA_PRECISION,nvl2(decode(t.DATA_SCALE,0,null,t.DATA_SCALE), "
					+ " '(' || t.DATA_PRECISION || ',' || t.DATA_SCALE || ')',  "
					+ " '(' || t.DATA_PRECISION || ')'),'(18)')),t.DATA_TYPE) AS jdbcType  "
 					+ "  FROM user_tab_columns t, user_col_comments c "
 					+ "  WHERE t.TABLE_NAME = c.table_name  "
 					+ "  AND t.COLUMN_NAME = c.column_name   "
 					+ "  AND t.TABLE_NAME = c.table_name  "
					+ " AND t.TABLE_NAME = upper(:tabName) "
					+ " ORDER BY t.COLUMN_ID ");			
		}
		//参数map
		HashMap<String,Object> parameters =new HashMap<String,Object> ();
		parameters.put("tabName", tablename);
		//HB查询标准原生sql
		columnList = this.findBySql(querySql.toString(),new Parameter(parameters));
				
		return columnList;
	}
	
}
